window.onload = async function () {
    const { data } = await get("/dashboard");

    for (const key in data.overview) {
        document.querySelector(`[name=${key}]`).innerHTML = data.overview[key];
    }

    // const { year } = data
    revenueChart(data.year)
    zu()
    oc(data.salaryData)
    salaryDistribution(data.salaryData)
    initMapChart(data.provinceData)

}


// 曲线

const revenueChart = function (data) {

    const month = data.map(item => {
        return item.month
    })

    const salary = data.map(item => {
        return item.salary
    })


    const line = document.querySelector('#line')
    let myChar = echarts.init(line);
    let option = {
        title: {
            text: "薪资曲线",
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        xAxis: {
            type: 'category',
            data: month,
            axisLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#ccc'
                }
            },
            axisLabel: {
                color: '#999'
            }
        },
        setOption: {
            grid: {
                top: '20%'
            }
        },

        color: [
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [
                    {
                        offset: 0,
                        color: '#499FEE' // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: '#5D75F0' // 100% 处的颜色
                    }
                ]
            }
        ],
        yAxis: {
            type: 'value'
        },
        splitLine: {
            lineStyle: {
                type: 'dashed'
            }
        },
        tooltip: {
            trigger: 'axis'
        },
        series: [
            {
                data: salary,
                type: 'line',
                smooth: true,
                lineStyle: {
                    width: 6,
                },
            }
        ]
    };

    option && myChar.setOption(option);
}
// 饼状
const oc = function (data) {

    const salary = document.querySelector('#salary')
    let myChar = echarts.init(salary);
    let option = {
        title: {
            text: "班级薪资分布",
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        legend: {
            bottom: '6%',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        color: ["#FDA224", "#5097FF", "#3ABCFA", "#34D39A"],
        series: [
            {

                type: 'pie',
                radius: ['50%', '64%'],
                center: ["50%", "45%"],
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                labelLine: {
                    show: false
                },
                data: [
                    { value: 4, name: '1万以下' },
                    { value: 25, name: '1万-1.5万' },
                    { value: 21, name: '1.5万-2万' },
                    { value: 20, name: '2万以上' },
                ]
            }
        ]

    }
    option && myChar.setOption(option);
}
// 柱状图
const zu = function () {

    const lines = document.querySelector('#lines')

    let myChart = echarts.init(lines);
    let option = {
        xAxis: {
            type: 'category',
            data: [
                'Mon',
                'Tue',
                'Wed',
                'Thu',
                'Fri',
                'Sat',
                'Sun',
                'ooi',
                'Sun',
                'ooi'
            ],
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed'
                }
            },
            axisLabel: {
                color: '#999'
            }
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        tooltip: {
            trigger: 'item'
        },
        grid: {
            left: 70,
            top: 30,
            right: 30,
            bottom: 50
        },
        color: [
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                    {
                        offset: 0,
                        color: '#34D39A' // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: 'rgba(52,211,154,0.2)' // 100% 处的颜色
                    }
                ]
            },
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                    {
                        offset: 0,
                        color: '#499FEE' // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: 'rgba(73,159,238,0.2)' // 100% 处的颜色
                    }
                ]
            }
        ],
        series: [
            {
                data: [120, 200, 150, 80, 70, 110, 130, 70, 110, 130],
                type: 'bar'
            },
            {
                data: [110, 130, 70, 110, 130, 120, 200, 150, 80, 70],
                type: 'bar'
            }
        ]
    };

    option && myChart.setOption(option);
}
// 男女薪资分布
const salaryDistribution = function (data) {
    let chartDom = document.getElementById('gender');
    let myChart = echarts.init(chartDom);
    let option;
    let name = data.map(item=>{
         return {name:item.label,value:item.b_count}
    })
    let value = data.map(item=>{
        return {name:item.label,value:item.g_count}
   })
    console.log(name);
    option = {
        title: [
            {
                text: '男',
                left: 'center',
                top: '45%',
                textStyle: {
                    fontSize: 12,
                  },
            },
            {
                text: '女',
                left: '50%',
                top: '92%',
                textStyle: {
                    fontSize: 12,
                  },
            }
        ],
        tooltip: {
            trigger: 'item'
        },
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
        series: [
            {
                bottom: '50%',
                radius: ['50%', '64%'],
                center: ['50%', '45%'],
                type: 'pie',
                data: value,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            },
            {
                top: '50%',
                radius: ['50%', '64%'],
                center: ['50%', '45%'],
                type: 'pie',
                data:name,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };

    option && myChart.setOption(option);
}
// 籍贯分布
const initMapChart = (data) => {
    const myEchart = echarts.init(document.querySelector("#map"));
    // const randomValue = () => {
    //   return parseInt(Math.random() * 20)
    // }
    // const dataList = [
    //   { name: '南海诸岛', value: 0 },
    //   { name: '北京', value: randomValue() },
    //   { name: '天津', value: randomValue() },
    //   { name: '上海', value: randomValue() },
    //   { name: '重庆', value: randomValue() },
    //   { name: '河北', value: randomValue() },
    //   { name: '河南', value: randomValue() },
    //   { name: '云南', value: randomValue() },
    //   { name: '辽宁', value: randomValue() },
    //   { name: '黑龙江', value: randomValue() },
    //   { name: '湖南', value: randomValue() },
    //   { name: '安徽', value: randomValue() },
    //   { name: '山东', value: randomValue() },
    //   { name: '新疆', value: randomValue() },
    //   { name: '江苏', value: randomValue() },
    //   { name: '浙江', value: randomValue() },
    //   { name: '江西', value: randomValue() },
    //   { name: '湖北', value: randomValue() },
    //   { name: '广西', value: randomValue() },
    //   { name: '甘肃', value: randomValue() },
    //   { name: '山西', value: randomValue() },
    //   { name: '内蒙古', value: randomValue() },
    //   { name: '陕西', value: randomValue() },
    //   { name: '吉林', value: randomValue() },
    //   { name: '福建', value: randomValue() },
    //   { name: '贵州', value: randomValue() },
    //   { name: '广东', value: randomValue() },
    //   { name: '青海', value: randomValue() },
    //   { name: '西藏', value: randomValue() },
    //   { name: '四川', value: randomValue() },
    //   { name: '宁夏', value: randomValue() },
    //   { name: '海南', value: randomValue() },
    //   { name: '台湾', value: randomValue() },
    //   { name: '香港', value: randomValue() },
    //   { name: '澳门', value: randomValue() },
    // ]
    const dataList = [
      { name: "南海诸岛", value: 0 },
      { name: "北京", value: 0 },
      { name: "天津", value: 0 },
      { name: "上海", value: 0 },
      { name: "重庆", value: 0 },
      { name: "河北", value: 0 },
      { name: "河南", value: 0 },
      { name: "云南", value: 0 },
      { name: "辽宁", value: 0 },
      { name: "黑龙江", value: 0 },
      { name: "湖南", value: 0 },
      { name: "安徽", value: 0 },
      { name: "山东", value: 0 },
      { name: "新疆", value: 0 },
      { name: "江苏", value: 0 },
      { name: "浙江", value: 0 },
      { name: "江西", value: 0 },
      { name: "湖北", value: 0 },
      { name: "广西", value: 0 },
      { name: "甘肃", value: 0 },
      { name: "山西", value: 0 },
      { name: "内蒙古", value: 0 },
      { name: "陕西", value: 0 },
      { name: "吉林", value: 0 },
      { name: "福建", value: 0 },
      { name: "贵州", value: 0 },
      { name: "广东", value: 0 },
      { name: "青海", value: 0 },
      { name: "西藏", value: 0 },
      { name: "四川", value: 0 },
      { name: "宁夏", value: 0 },
      { name: "海南", value: 0 },
      { name: "台湾", value: 0 },
      { name: "香港", value: 0 },
      { name: "澳门", value: 0 },
    ];
    dataList.forEach((item) => {
      const obj = data.find(
        (it) =>
          it.name.replace(
            /省|回族自治区|吾尔自治区|壮族自治区|特别行政区|自治区/g,
            ""
          ) === item.name
      );
      if (obj) item.value = obj.value;
    });
    let option = {
      title: {
        text: "籍贯分布",
        top: 10,
        left: 10,
        textStyle: {
          fontSize: 16,
        },
      },
      tooltip: {
        trigger: "item",
        formatter: "{b}: {c} 位学员",
        borderColor: "transparent",
        backgroundColor: "rgba(0,0,0,0.5)",
        textStyle: {
          color: "#fff",
        },
      },
      visualMap: {
        min: 0,
        max: 6,
        left: "left",
        bottom: "20",
        text: ["6", "0"],
        inRange: {
          color: ["#ffffff", "#0075F0"],
        },
        show: true,
        left: 40,
      },
      geo: {
        map: "china",
        roam: false,
        zoom: 1.0,
        label: {
          normal: {
            show: true,
            fontSize: "10",
            color: "rgba(0,0,0,0.7)",
          },
        },
        itemStyle: {
          normal: {
            borderColor: "rgba(0, 0, 0, 0.2)",
            color: "#e0ffff",
          },
          emphasis: {
            areaColor: "#34D39A",
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 20,
            borderWidth: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)",
          },
        },
      },
      series: [
        {
          name: "籍贯分布",
          type: "map",
          geoIndex: 0,
          data: dataList,
        },
      ],
    };
    myEchart.setOption(option);
  };

